'use strict';

var baseUrl = 'http://localhost:8080/simon-api';

// Alerts
angular.module('simon.services').factory('AlertFactory', function ($rootScope, $timeout) {
	var factory = null;
    $rootScope.alerts = []; // Array of alerts available globally
    return factory = {
		add: function(type, msg) {
			factory.clear();
			return $rootScope.alerts.push({
				type: type,
				msg: msg,
				close: function() {
					return factory.closeAlert(this);
				}
			});
		},
		closeAlert: function(alert) {
			return this.closeAlertIdx($rootScope.alerts.indexOf(alert));
		},
		closeAlertIdx: function(index) {
			return $rootScope.alerts.splice(index, 1);
		},
		clear: function(){
			$rootScope.alerts = [];
		}
    };
});

// Modais
angular.module('simon.services').factory('ModalFactory', function ($modal) {
    return {
		error : function(header, msg) {
			return $modal.open({
				templateUrl : '/dialogs/error.html',
				controller : 'errorDialogCtrl',
				resolve : {
					header : function() { return angular.copy(header); },
					msg : function() { return angular.copy(msg); }
				}
			}); // end modal.open
		}, // end error
		notify : function(header, msg) {
			return $modal.open({
				templateUrl : '/dialogs/notify.html',
				controller : 'notifyDialogCtrl',
				resolve : {
					header : function() { return angular.copy(header); },
					msg : function() { return angular.copy(msg); }
				}
			}); // end modal.open
		},
		confirm : function() {
			return $modal.open({
				templateUrl : '/dialogs/confirm.html',
				controller : 'confirmDialogCtrl'
			}); // end modal.open
		}
    };
});

// Login
angular.module('simon.services').factory('LoginFactory', function ($resource) {
	return $resource(baseUrl + '/login/:login/:senha', {}, {
    	show: { method: 'GET', params: {login: '@login', senha: '@senha'} }
    });
});

// Clientes
angular.module('simon.services').factory('ClienteFactory', function ($resource) {
    return $resource(baseUrl + '/cliente', {}, {
        query: { method: 'GET', isArray: true },
        create: { method: 'POST' }
    });
});

angular.module('simon.services').factory('ClienteIdFactory', function ($resource) {
    return $resource(baseUrl + '/cliente/:id', {}, {
        show: { method: 'GET', params: {id: '@id'} },
        update: { method: 'PUT', params: {id: '@id'} },
        remove: { method: 'DELETE', params: {id: '@id'} }
    });
});

// Fornecedores
angular.module('simon.services').factory('FornecedorFactory', function ($resource) {
    return $resource(baseUrl + '/fornecedor', {}, {
        query: { method: 'GET', isArray: true },
        create: { method: 'POST' }
    });
});

angular.module('simon.services').factory('FornecedorIdFactory', function ($resource) {
    return $resource(baseUrl + '/fornecedor/:id', {}, {
        show: { method: 'GET', params: {id: '@id'} },
        update: { method: 'PUT', params: {id: '@id'} },
        remove: { method: 'DELETE', params: {id: '@id'} }
    });
});

// Funcionarios
angular.module('simon.services').factory('FuncionarioFactory', function ($resource) {
    return $resource(baseUrl + '/funcionario', {}, {
        query: { method: 'GET', isArray: true },
        create: { method: 'POST' }
    });
});

angular.module('simon.services').factory('FuncionarioIdFactory', function ($resource) {
    return $resource(baseUrl + '/funcionario/:id', {}, {
        show: { method: 'GET', params: {id: '@id'} },
        update: { method: 'PUT', params: {id: '@id'} },
        remove: { method: 'DELETE', params: {id: '@id'} }
    });
});

// Condicoes de Pagamento
angular.module('simon.services').factory('CondPagtoFactory', function ($resource) {
    return $resource(baseUrl + '/condPagto', {}, {
        query: { method: 'GET', isArray: true },
        create: { method: 'POST' }
    });
});

angular.module('simon.services').factory('CondPagtoIdFactory', function ($resource) {
    return $resource(baseUrl + '/condPagto/:id', {}, {
        show: { method: 'GET', params: {id: '@id'} },
        update: { method: 'PUT', params: {id: '@id'} },
        remove: { method: 'DELETE', params: {id: '@id'} }
    });
});

angular.module('simon.services').factory('CondPagtoValorFactory', function ($http) {
    return {
    	query: function(valor, idFormPagto, parcelas) {
    		return $http({method: "GET", url: baseUrl + '/condPagto/valor', params: {valor: valor, idFormPagto: idFormPagto, parcelas: parcelas}});
    	}
    };
});

// Produtos
angular.module('simon.services').factory('ProdutoFactory', function ($resource) {
    return $resource(baseUrl + '/produto', {}, {
        query: { method: 'GET', isArray: true },
        create: { method: 'POST' }
    });
});

angular.module('simon.services').factory('ProdutoIdFactory', function ($resource) {
    return $resource(baseUrl + '/produto/:id', {}, {
        show: { method: 'GET', params: {id: '@id'} },
        update: { method: 'PUT', params: {id: '@id'} },
        remove: { method: 'DELETE', params: {id: '@id'} }
    });
});

// Usuarios
angular.module('simon.services').factory('UsuarioFactory', function ($resource) {
    return $resource(baseUrl + '/usuario', {}, {
        query: { method: 'GET', isArray: true },
        create: { method: 'POST' }
    });
});

angular.module('simon.services').factory('UsuarioIdFactory', function ($resource) {
    return $resource(baseUrl + '/usuario/:id', {}, {
        show: { method: 'GET', params: {id: '@id'} },
        update: { method: 'PUT', params: {id: '@id'} },
        remove: { method: 'DELETE', params: {id: '@id'} }
    });
});

// Lojas
angular.module('simon.services').factory('LojaFactory', function ($resource) {
    return $resource(baseUrl + '/loja', {}, {
        query: { method: 'GET', isArray: true }
    });
});

// Estados
angular.module('simon.services').factory('EstadoFactory', function ($resource) {
    return $resource(baseUrl + '/estado', {}, {
        query: { method: 'GET', isArray: true }
    });
});

// Cidades
angular.module('simon.services').factory('CidadeFactory', function ($resource) {
    return $resource(baseUrl + '/cidade/:id', {}, {
    	query: { method: 'GET', params: {id: '@id'}, isArray: true }
    });
});
angular.module('simon.services').factory('CidadeCepFactory', function ($resource) {
    return $resource(baseUrl + '/cidade/cep/:cep', {}, {
    	query: { method: 'GET', params: {id: '@cep'}, isArray: true }
    });
});

// Enderecos
angular.module('simon.services').factory('EnderecoFactory', function ($resource) {
    return $resource(baseUrl + '/endereco/:cep', {}, {
    	show: { method: 'GET', params: {id: '@cep'} }
    });
});

// Formas de Pagamento
angular.module('simon.services').factory('FormPagtoFactory', function ($resource) {
    return $resource(baseUrl + '/formPagto', {}, {
        query: { method: 'GET', isArray: true },
    	queryAtivos: { method: 'GET', url: baseUrl + '/formPagto/queryAtivos', isArray: true }
    });
});

// Venda
angular.module('simon.services').factory('VendaFactory', function ($resource) {
	return $resource(baseUrl + '/venda', {}, {
		query: { method: 'GET', params: {dataIni: '@dataIni', dataFim: '@dataFim'}, isArray: true },
		create: { method: 'POST' }
    });
});